package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.ReservationService;
import com.zzyl.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer")
@Slf4j
@Api(tags = "客户端：预约")
public class CustomerReservationController {

    @Autowired
    private ReservationService reservationService;

    @GetMapping("/reservation/cancelled-count")
    @ApiOperation("查询取消预约人数")
    public ResponseResult<Integer> select(){

        Integer count = reservationService.select();

        return ResponseResult.success(count);
    }

    @GetMapping("/reservation/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult<List<ReservationVo>> countByTime(@DateTimeFormat(pattern = "yyyy-MM-dd")Long time){

        List<ReservationVo> list = reservationService.countByTime(time);

        return ResponseResult.success(list);
    }

    @PostMapping("/reservation")
    @ApiOperation("新增预约")
    public ResponseResult update(@RequestBody ReservationDto reservationDto){

        reservationService.insert(reservationDto);

        return ResponseResult.success();
    }

    @GetMapping("/reservation/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<ReservationVo>> searchByPage(@ApiParam(value = "页码",required = true) Integer pageNum,
                                                                    @ApiParam(value = "每页的数量",required = true) Integer pageSize,
                                                                    @ApiParam(value = "预约状态，0：待报道，1：已完成，2：取消，3：过期") Integer status){

        PageResponse<ReservationVo> page = reservationService.searchByPage(pageNum,pageSize,status);

        return ResponseResult.success(page);
    }

    @PutMapping("/reservation/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult cancel(@PathVariable Long id){

        reservationService.cancel(id);

        return ResponseResult.success();
    }

    @Scheduled(cron = "* 1/30 * * * ?")
    @ApiOperation("自动执行预约过期")
    public ResponseResult autoStatus(){
        reservationService.autoStatus();

        return ResponseResult.success();
    }
}
